package Flap::Schema::Storage::MySQL;
use Flap qw/-b Flap::Schema::Storage -m -s -w/;
use constant AUTO_INFLATE_T => {
    date      => 'date',
    datetime  => 'time',
    timestamp => 'time',
};

sub create_database_sql {
    my $dbn = shift->dbname;
    "CREATE DATABASE $dbn DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;\n";
}

sub create_table_sql {
    my($self, $sch) = @_;
    my $opt = $sch->table_option;
    my $e = $opt->{engine};
    local $opt->{suffix} = ($opt->{suffix} ? "$opt->{suffix} " : '')."ENGINE=$e" if $e;
    $self->SUPER::create_table_sql($sch);
}

sub create_table_sql_pk {
    my($self, $sch) = @_;
    my $ci = $sch->column_info($sch->columns->[0]);
    return if $ci->{-t} eq 'serial';
    $self->SUPER::create_table_sql_pk($sch);
}

sub create_user_sql {
    my $self = shift;
    my $dbn = $self->dbname;
    my $un = $self->user;
    my $pw = $self->password;
    $pw = " IDENTIFIED BY '$pw'" if $pw ne '';
    
    my $sql = '';
    foreach(@{$self->config->allow_from}){
        $sql .= "GRANT ALL PRIVILEGES ON $dbn.* TO '$un'\@'$_'$pw WITH GRANT OPTION;\n";
    }
    $sql;
}

sub deflate_date { $_[1]->ymd }

sub deflate_time { $_[1]->ymd.' '.$_[1]->hms }

sub disable_constraint_sql { "SET foreign_key_checks=0;\n" }

sub enable_constraint_sql { "SET foreign_key_checks=1;\n" }

1;
